package Controller;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Entity.User;
import Model.LoginModel;
import View.LoginView;
import Controller.*;

public class LoginController {
	private LoginView view = null;
	private LoginModel model = null;
	private JFrame frame;
	public String rolesMana = "Manager";
	public String rolesEmp="Employee";
	public static String name;

	public LoginController() {
		frame = new JFrame();
		view = new LoginView();
		model = new LoginModel();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(view);
		Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		frame.setBounds(screensize.width/2-150,screensize.height/2 - 90,300,180);
		frame.setVisible(true);
		frame.setResizable(false);
		doAction();
	}

	private void doAction() {
		// TODO Auto-generated method stub
		doLogin();
		doExit();
	}

	private void doExit() {
		// TODO Auto-generated method stub
		view.doExit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				frame.dispose();
			}
		});
	}

	private void doLogin() {
		// TODO Auto-generated method stub
		view.doLogin(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				boolean validateUser = false;
				try {
					validateUser = model.validateUsers(view.getUser());
					LoginController.name = view.getUser().getUsername();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				if (!validateUser) {
					JOptionPane.showMessageDialog(frame,
							"Tên đăng nhập hoặc mật khẩu không đúng");
					return;
				}
				try {
					if (model.getUser(view.getUser()).getRoles().equals(rolesMana) ){
						new ManagerController();
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					if (model.getUser(view.getUser()).getRoles().equals(rolesEmp)){
						new EmployeeController();
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
//				new ManagerController();
				frame.dispose();
			}
		});
	}

	public static void main(String args[]) {
		new LoginController();
		//System.out.println("abc");
	}
}
